//
// Created by isabella on 25-5-30.
//
#include <common.h>

class Solution {
    vector<int> nums;
    int len;
public:

    Solution(const vector<int> & _nums) {
        this->nums = _nums;
        ranges::sort(this->nums);
        this->len = _nums.size();
    }

    /**
     * 等于搜索
     * @return
     */
    int eq(int target) {
        int left = 0, right = len - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            }else if (nums[mid] < target) {
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return -1;
    }

    /**
     * 小于等于搜索
     * @return
     */
    int lslant(int target) {
        int left = 0, right = len - 1;
        int result = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if(nums[mid]>target) {
                right = mid - 1;
            }else {
                result = mid;
                left = mid + 1;
            }
        }
        return result;
    }

    /**
     * 大于等于搜索
     * @return
     */
    int gslant(int target) {
        int left = 0, right = len - 1;
        int result = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if(nums[mid]<target) {
                left = mid + 1;
            }else {
                result = mid;
                right = mid - 1;
            }
        }
        return result;
    }

    /**
     * 小于搜索
     * @return
     */
    int less(int target) {
        int left = 0, right = len - 1;
        int result = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if(nums[mid]>=target) {
                right = mid - 1;
            }else {
                result = mid;
                left = mid + 1;
            }
        }
        return result;
    }

    /**
     * 大于搜索
     * @return
     */
    int greater(int target) {
        int left = 0, right = len - 1;
        int result = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if(nums[mid]<=target) {
                left = mid + 1;
            }else {
                result = mid;
                right = mid - 1;
            }
        }
        return result;
    }
};
int main() {
    vector<int> nums = {0,1,2,3,4};
    auto solution = Solution(nums);
    for(int i=-2;i<6;i++) {
        cout<<"-------------------"<<endl;
        cout<<"等于搜索：("<<i<<")="<<solution.eq(i)<<endl;
        cout<<"小于等于搜索：("<<i<<")="<<solution.lslant(i)<<endl;
        cout<<"大于等于等于搜索：("<<i<<")="<<solution.gslant(i)<<endl;
        cout<<"小于搜索：("<<i<<")="<<solution.less(i)<<endl;
        cout<<"大于搜索：("<<i<<")="<<solution.greater(i)<<endl;
        cout<<"-------------------"<<endl;
    }
    return 0;
}
